// 连接数据库
const mongoose = require('mongoose')

const userModel = require('../models/userModel')

mongoose.connect('mongodb://127.0.0.1:27017/books_test', {
  useNewUrlParser: true,
  useUnifiedTopology: true,
})

//2.连接成功
mongoose.connection.once('connected', function () {
  console.log('连接成功：', 'mongodb://127.0.0.1:27017/books_test')
  userModel.create({
    username: 'admin',
    password: '123456',
  })
})
//3.连接失败
mongoose.connection.on('error', function (err) {
  console.log('连接错误：', err)
})
//4.断开连接
mongoose.connection.on('disconnection', function () {
  console.log('断开连接')
})
module.exports = mongoose
